<?xml version="1.0" encoding="UTF-8"?>

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                xmlns:aui="http://liferay.com/faces/aui"
                xmlns:portlet="http://java.sun.com/portlet_2_0">

    <div class="container-fluid">

        <div style="float: left;">
            <h1>#{iFeedModelBean.name}

                <h:panelGroup rendered="#{not empty iFeedModelBean.id}">(
                    <h:outputText value="Id: "></h:outputText>
                    <h:outputText value="#{iFeedModelBean.id}"></h:outputText>
                    )
                </h:panelGroup>
            </h1>

            <div>
                <a id="basicFeedInfoDisplayLink" style="display:none; cursor: pointer;"
                   onclick="toggle('basicFeedInfo', 'basicFeedInfoDisplayLink', 'basicFeedInfoHideLink');">
                    Visa detaljer
                </a>
                <a id="basicFeedInfoHideLink" style="cursor: pointer;"
                   onclick="toggle('basicFeedInfo', 'basicFeedInfoDisplayLink', 'basicFeedInfoHideLink');">
                    Dölj detaljer
                </a>
            </div>
        </div>

        <div class="ifeed-buttons" style="float: right">
            <p:commandLink styleClass="rp-link-button secondary" value="Avbryt" update=":ifeed_view"
                           rendered="#{!app.inEditMode}"
                           id="abortEditIfeedLink"
                           ajax="false" async="true"
                           actionListener="#{navigationModelBean.setUiNavigation('USER_IFEEDS')}">
            </p:commandLink>
            <p:commandLink styleClass="rp-link-button rp-link-button-alt" value="Ändra" update=":ifeed_view"
                           rendered="#{not app.inEditMode and app.mayEditFeed(request, iFeedModelBean)}"
                           id="startEditIfeedLink"
                           ajax="false" async="true" actionListener="#{app.setInEditMode(!app.isInEditMode())}">
            </p:commandLink>
            <p:commandLink styleClass="rp-link-button secondary" value="Avbryt" update=":ifeed_view"
                           rendered="#{app.inEditMode}"
                           id="cancelEditIfeedLink"
                           ajax="false" async="true" actionListener="#{app.setInEditMode(!app.isInEditMode())}">
            </p:commandLink>
            <p:commandLink styleClass="rp-link-button rp-link-button-alt" value="Spara" update=":ifeed_view"
                           rendered="#{app.isInEditMode()}"
                           id="updateFeedLink"
                           ajax="false" async="true" actionListener="#{app.update()}">
            </p:commandLink>
        </div>


        <div id="basicFeedInfo">

            <h:panelGroup layout="block" styleClass="row-fluid border" style="display:block;" id="basicFeedInfo">
                <div class="span4 first">
                    <h:panelGroup>
                        <h:outputText style="font-weight: bold;" value="Namn: "></h:outputText>
                        <br/>
                        <h:outputText value="#{iFeedModelBean.name}" label="text" rendered="#{not app.inEditMode}"/>
                        <h:inputText value="#{iFeedModelBean.name}" label="text" rendered="#{app.inEditMode}"
                                     id="nameOfFeedInput">
                        </h:inputText>
                    </h:panelGroup>

                    <h:panelGroup>
                        <br/>
                        <h:outputText style="font-weight: bold;" value="Beskrivning: "></h:outputText>
                        <br/>
                        <h:outputText value="#{iFeedModelBean.description}" label="text"
                                      rendered="#{not app.inEditMode}"/>
                        <h:inputText value="#{iFeedModelBean.description}" label="text" rendered="#{app.inEditMode}"
                                     id="descriptionInput">
                        </h:inputText>
                    </h:panelGroup>

                    <h:panelGroup>
                        <br/>
                        <span style="font-weight: bold;"><i class="icon-user"></i>Ägare:</span><br/>
                        <h:outputText
                                value="#{app.fetchLdapPersonFullName(iFeedModelBean.userId)} (#{iFeedModelBean.userId})"
                                rendered="#{not app.inEditMode}"/>
                        <p:autoComplete id="createOwnershipInput"
                                        value="#{iFeedModelBean.userId}"
                                        completeMethod="#{app.completeUserName}"
                                        forceSelection="true"
                                        rendered="#{app.inEditMode}"
                                        title="Skriv in vgr-id för ny ägare här. Observera att du kan bli av med möjligheten att ändra posten om du inte samtidigt lagt till dig själv som administratör innan du sparar ändringen."
                                        multiple="false"/>
                        <p:watermark for="createOwnershipInput" value="Ändra ägare här" />
                    </h:panelGroup>


                    <div>
                        <div style="font-weight: bold;"><i class="icon-user"></i>Administratörer:
                        </div>
                        <p:autoComplete id="createNewAdminInput"
                                        value="#{app.newOwnershipUserId}"
                                        completeMethod="#{app.completeUserName}"
                                        forceSelection="true"
                                        rendered="#{app.inEditMode}"
                                        styleClass="createNewAdminInput master-item"
                                        title="Skriv in vgr-id och klicka på 'Lägg till'' för att lägg till en användare som administratör på feedet."
                                        multiple="false" />
                        <p:watermark for="createNewAdminInput" value="Vgr-Id för ny admin" />

                        <p:commandLink styleClass="ltr-flowing-item"
                                       value="Lägg till"
                                       update="basicFeedInfo"
                                       rendered="#{app.inEditMode}"
                                       id="addOwnershipLink"
                                       ajax="true" async="true" actionListener="#{app.createNewOwnership()}">
                        </p:commandLink>

                        <ui:repeat value="#{iFeedModelBean.getOwnershipsAsList()}" var="getOwnershipsAsListItem"
                                   idFoo="owners"
                                   varStatus="status">
                          <span class="ltr-flowing-item">
                            <span class="name">#{app.fetchLdapPersonFullName(getOwnershipsAsListItem.userId)} (#{getOwnershipsAsListItem.userId})</span>
                            <span class="remove">
                              <p:commandLink value=" " update=":ifeed_view"
                                             rendered="#{app.inEditMode}"
                                             styleClass="link-button-trash"
                                             ajax="false" async="true"
                                             id="remove_#{getOwnershipsAsListItem.userId}"
                                             actionListener="#{iFeedModelBean.getOwnershipsAsList().remove(getOwnershipsAsListItem)}">
                              </p:commandLink>
                            </span>
                          </span>
                        </ui:repeat>
                    </div>


                </div>


                <div class="span4">
                    <ui:include src="show-department-and-group.xhtml"/>
                </div>
                <div class="span4 last">
                    <h:outputText style="font-weight: bold;" value="Lägg till: "></h:outputText>
                    <br/>
                    <h:commandLink value="EPI-konfiguration (JSON)"
                                   actionListener="#{tableDef.editNewJsonp()}"
                                   ajax="true"
                                   id="epiConfigLink"
                                   update=":ifeed_view"
                                   immediate="true">
                    </h:commandLink>
                    <br/>
                    <h:outputText style="font-weight: bold;" value="Länkar till dokumentlista: "></h:outputText>
                    <br/>
                    <a href="#{app.getWebFeedLink()}" target="_blank">Html</a><br/>
                    <a id="excel-feed-link" href="#{app.getExcelFeedLink()}" target="_blank">Excel</a><br/>
                    <a href="#{app.getRssFeedLink()}" target="_blank">Rss</a><br/>
                    <a href="#{app.getAtomFeedLink()}" target="_blank">Atom</a><br/>
                    <a id="json-feed-link" href="#{app.getJsonFeedLink()}" target="_blank">Json</a><br/>

                    <h:panelGroup>
                        <h:outputText style="font-weight: bold;" value="Länkad till orginaldokument i html-vyn: "/>
                        <br/>
                        <h:outputText value="#{ (iFeedModelBean.linkNativeDocument==true) ? 'Ja' : 'Nej' }"
                                      rendered="#{not app.inEditMode}"/>
                        <h:selectBooleanCheckbox value="#{iFeedModelBean.linkNativeDocument}"
                                                 rendered="#{app.inEditMode}"
                                                 id="linkNativeDocumentCheckBox"
                                                 ajax="true"/>
                    </h:panelGroup>

                </div>
            </h:panelGroup>

        </div>
    </div>

    <div class="container-fluid">
        <h3 style="display: inline;">
            EPI-konfigurationer (JSON):
        </h3>
        <ui:repeat value="#{iFeedModelBean.dynamicTableDefs}" var="ifeedDynamicTableDefsItem" varStatus="status"
                   id="ifeedDynamicTableDefsRepeat">
            <span class="ltr-flowing-item ifeed-border">
              <span class="name">#{ifeedDynamicTableDefsItem.name}</span>
              <span class="edit">
                <p:commandLink styleClass="link-button-edit" value=" " update=":ifeed_view"
                               style="color: white;"
                               ajax="false" async="true"
                               action="#{tableDef.editFlow(ifeedDynamicTableDefsItem)}">
                </p:commandLink>
              </span>

              <span class="remove">
                <p:commandLink styleClass="link-button-trash" value=" " update=":ifeed_view"
                               style="color: white;"
                               rendered="#{app.inEditMode}"
                               ajax="false" async="true"
                               action="#{iFeedModelBean.removeFlow(ifeedDynamicTableDefsItem)}">
                </p:commandLink>
              </span>
            </span>
        </ui:repeat>
    </div>


    <div class="container-fluid">
        <h3 style="display: inline;">
            Filter:
        </h3>
        <ui:repeat value="#{iFeedModelBean.getFiltersAsList()}" var="getFiltersAsListItem" varStatus="status"
                   id="getFiltersAsListRepeat">
    <span class="ltr-flowing-item ifeed-border">
      <span class="name">#{app.fieldsByNameIndex.get(getFiltersAsListItem.filterKey).name}</span>
      <span class="name-value-delimiter">:</span>
      <span>#{empty getFiltersAsListItem.filterQueryForDisplay ? getFiltersAsListItem.filterQuery : getFiltersAsListItem.filterQueryForDisplay}</span>

      <span class="edit">
        <p:commandLink styleClass="link-button-edit" value=" " update=":ifeed_view"
                       style="color: white;"
                       rendered="#{app.inEditMode}"
                       ajax="false" async="true"
                       action="#{app.editFilterValue(getFiltersAsListItem)}">
        </p:commandLink>
      </span>

      <span class="remove">
        <p:commandLink styleClass="link-button-trash" value=" " update=":ifeed_view"
                       style="color: white;"
                       rendered="#{app.inEditMode}"
                       ajax="false" async="true"
                       action="#{iFeedModelBean.removeFilter(getFiltersAsListItem)}">
        </p:commandLink>
      </span>
    </span>
        </ui:repeat>
    </div>


    <div class="container-fluid">

        <h:panelGroup layout="block" styleClass="row-fluid" rendered="#{app.inEditMode}">
            <div class="span4 first">
                <ui:include src="add-filter.xhtml"/>
            </div>
            <div class="span4">
                <ui:include src="add-filter-content.xhtml"></ui:include>
            </div>
            <div class="span4 last">
                <ui:include src="results.xhtml"/>
            </div>
        </h:panelGroup>

        <h:panelGroup layout="block" styleClass="row-fluid" rendered="#{!app.inEditMode}">
            <div class="span6 first">
                <ui:include src="results.xhtml"/>
            </div>
            <div class="span6 last">
                &#160;
            </div>
        </h:panelGroup>

    </div>


    <script type="text/javascript">
    //<![CDATA[

        function toggle() {
			var dta = 'display-toggle'; // name of the attribute... for brievety.
           for (var i = 0; i < arguments.length; i++) {
               var id = arguments[i];
               $('#' + id).toggle();
               //var e = document.getElementById(id);
			   //var displayToggle = e.hasAttribute(dta) ? e.getAttribute(dta) : (e.style.display != 'none' ? 'none' : '');
			   //e.setAttribute(dta, e.style.display);
			   //e.style.display = displayToggle;
            }
        }

        function refreshResults() {
            console.log('refreshResults before');
            setTimeout(function () {document.getElementById('rerun-ifeed-loading').click();}, 500);
            console.log('refreshResults after');
        }
    //]]>
    </script>

</ui:composition>